package com.zmzncs.lmtc.module.promotion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmzncs.lmtc.common.util.JwtUtil;
import com.zmzncs.lmtc.module.promotion.pojo.recharge_rule.RechargeRule;
import com.zmzncs.lmtc.module.promotion.dao.RechargeRuleDao;
import com.zmzncs.lmtc.module.promotion.service.RechargeRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.module.system.pojo.user.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 促销-充值规则 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2021-03-02
 */
@Service
public class RechargeRuleServiceImpl extends ServiceImpl<RechargeRuleDao, RechargeRule> implements RechargeRuleService {

    @Autowired
    RechargeRuleDao rechargeRuleDao;
    @Autowired
    HttpServletRequest request;


    @Override
    public List<String> getRechargeRule() {
        return rechargeRuleDao.getRechargeRule();
    }

    @Override
    public void updateRechargeRule(List<String> recharges) {

        String token = request.getHeader("token");
        UserLoginVO loginUser = JwtUtil.getLoginUser(token);
        Long userId = loginUser.getUserId();


        if (recharges != null && recharges.size() > 0){

            LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper();
            rechargeRuleDao.delete(queryWrapper);

            Collections.sort(recharges);
            for (String recharge : recharges) {
                RechargeRule rule = new RechargeRule();
                rule.setCreateUserId(userId);
                rule.setRechargePrice( new BigDecimal(recharge));
                rule.setSellingPrice(new BigDecimal(recharge));
                rule.setStatus(1);
                rechargeRuleDao.insert(rule);
            }
        }
    }
}
